// pages/userForm/userForm.js

const {
  articlePageTitle,
  hiddenArticleIdList,
  adminWxOpenid,
} = require("../../config");



Page({

  /**
   * 页面的初始数据
   */
  data: {
    articleDoc: getApp().globalData.articleDoc,
    // curPageArticleList: [{_id:'xxx'}],
    curPageArticleList: [],
    pageSize: 10,
    articlePageTitle,
    isAdmin: getApp().globalData.isAdmin,
  },

  processCurPageArticleList(curPageArticleList) {
    const publicArticleList = [];
    const adminArticleList = []
    // if(getApp().globalData.isAdmin)
    curPageArticleList.forEach(item => {
      // item.article.updatedTime = getFormatedDate(item.article.updatedTime)
      // item.article.updatedTime = item.article.updatedTime.replace(/\s+.*/, ''); 
      if (item.show === true) {
        publicArticleList.push(item);
      } else {
        adminArticleList.push(item);
      }
    });
    let tmpArticleList = [];
    if (getApp().globalData.isAdmin) {
      tmpArticleList = [...adminArticleList, ...publicArticleList];
    } else {
      tmpArticleList = publicArticleList;
    }
    this.setData({
      curPageArticleList: tmpArticleList,
    });
  },


  articleDetail(e) {
    const _id = e.currentTarget.dataset.id;
    console.log(`53-53 articleList.js:::::articleDetail:::_id: ${_id}`);
    wx.navigateTo({
      url: `/pages/articleDetail/articleDetail?_id=${_id}`,
      success: (res) => {},
      fail: (err) => {
        console.log('18-18 articleList.js:::::articleDetail:::fail::err:', err);
      },
      complete: (res) => {},
    });
  },

  editArticle(e) {
    const _id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/articleForm/articleForm?_id=${_id}`,
      success: (res) => {},
      fail: (err) => {
        console.log('18-18 articleList.js:::::editArticle:::fail::err:', err);
      },
      complete: (res) => {},
    });
  },

  addArticle() {
    wx.navigateTo({
      url: `/pages/articleForm/articleForm`,
      success: (res) => {},
      fail: (err) => {
        console.log('18-18 articleList.js:::::addArticle:::fail::err:', err);
      },
      complete: (res) => {},
    });
  },

  onLoad(options) {
    const page = options.page ? options.page : 1;
    this.setData({
      page
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: async function (options) {
    // const articleDoc = getApp().globalData.articleDoc;
    console.log(getApp().globalData.articleDoc, '20-20 articleList.js:::::onLoad::: articleDoc');
    const curPageArticleListRes = await this.getList(this.data.page);
    let curPageArticleList = curPageArticleListRes.data.reverse();
    if (/ok/.test(curPageArticleListRes.errMsg)) {
      curPageArticleList = this.processCurPageArticleList(curPageArticleList);
    } else {
      console.log('27-27 articleList:::::onLoad::: curPageArticleListRes.errMsg: ', curPageArticleListRes.errMsg);
    }
  },

  async getTotalRecordCount() {
    let totalRecordCount;
    if (getApp().globalData.isAdmin) {
      totalRecordCount = await getApp().globalData.articleDoc.count();
    } else {
      totalRecordCount = await getApp().globalData.articleDoc.where({
        'article.isAdmin': false,
      }).count();
    }
    return totalRecordCount;
  },

  async getList(page = 1) {
    // console.log(this.data, '33-33 articleList.js getList.js:::::this.data');
    let recordList;
    console.log('124-124 articleList.js getList.js:::::getApp().globalData.isAdmin:', getApp().globalData.isAdmin);
    if (getApp().globalData.isAdmin) {
      recordList = await getApp().globalData.articleDoc.skip((page - 1) * this.data.pageSize).limit(this.data.pageSize).get()
      console.log(recordList, '126-126 isAdmin: true, articleList.js getList.js:::::getList');
    } else {
      recordList = await getApp().globalData.articleDoc.where({
        'article.needAdmin': false,
      }).skip((page - 1) * this.data.pageSize).limit(this.data.pageSize).get()
      console.log(recordList, '129-129 isAdmin: false, articleList.js getList.js:::::getList');
    }
    return recordList;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {

  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})